package org.example.config;

import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
import org.springframework.aop.config.AopConfigUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

@Component
public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    @Autowired
    private BeanFactory beanFactory;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        if (beanFactory.containsBeanDefinition(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME)) {
            if (beanFactory.containsBean(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME)) {
                Object bean = beanFactory.getBean(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME);
                AnnotationAwareAspectJAutoProxyCreator aspectJAutoProxyCreator = (AnnotationAwareAspectJAutoProxyCreator) bean;
                aspectJAutoProxyCreator.setInterceptorNames("customInteceptor");
                aspectJAutoProxyCreator.setCustomTargetSourceCreators();
            }
        }
    }
}
